package com.caul.demo.hadoop.mapreduce.ext;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 相同的数据项合并(在MapTask阶段, 溢出(输出)缓存对象的时候调用)
 *  慎用 注意可能对业务逻辑造成影响
 * Created by BlueDream on 2018-04-06.
 */
public class CustomCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        for (IntWritable value : values) {
            count += value.get();
        }
        context.write(key, new IntWritable(count));
    }
}
